package com.netease.nim.uikit.xpopup.popuview

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.VERTICAL
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils
import www.wen.com.common.R
import www.wen.com.common.lib.data.entities.ItemIsSelected
import www.wen.com.common.lib.helper.ResultCallBack

/**
 * @author WEN
 * @Description:
 * @date 2021/4/25 21:35
 */
abstract class BaseBottomListPopupView<T : ItemIsSelected>(
    context: Context,
    var data: List<T>,
    var resultCallBack: ResultCallBack<T>? = null,
) : BottomPopupView(context),
        OnItemClickListener {

    private var mCurrentPosition: Int = 0

    lateinit var mRvAdapter: BaseQuickAdapter<T, BaseViewHolder>

    private var mRecyclerView: RecyclerView? = null

    override fun onCreate() {
        super.onCreate()
        initView()
        initRvList()
    }

    private fun initView() {
        mRecyclerView = findViewById(R.id.rvList)
        if (mRecyclerView == null) throw IllegalArgumentException("布局资源必须拥有rvList的RecycleView的Id")
    }

    private fun initRvList() {

        mRecyclerView!!.run {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(getItemDecoration())
            adapter = getRvAdapter().apply {
                mRvAdapter = this
            }
            mRvAdapter.setList(data)
            mRvAdapter.setOnItemClickListener(this@BaseBottomListPopupView)
        }

        data.forEachIndexed { index, data ->
            if (data.isSelected) {
                mCurrentPosition = index
                return@forEachIndexed
            }
        }
    }

    open fun getListLayoutManager(): RecyclerView.LayoutManager = LinearLayoutManager(context)

    open fun getItemDecoration(): RecyclerView.ItemDecoration = DividerItemDecoration(context, VERTICAL)

    abstract fun getRvAdapter(): BaseQuickAdapter<T, BaseViewHolder>

    override fun getMaxHeight(): Int = (XPopupUtils.getScreenHeight(context) * 0.5f).toInt()

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        if (mCurrentPosition != position) {

            val currentEntity = (adapter.getItem(position)) as ItemIsSelected
            currentEntity.isSelected = true

            val oldEntity = (adapter.getItem(mCurrentPosition)) as ItemIsSelected
            oldEntity.isSelected = false

            adapter.notifyItemChanged(position)
            adapter.notifyItemChanged(mCurrentPosition)

            mCurrentPosition = position

            resultCallBack?.invoke(adapter.data[position] as T)
        }else{
            repeatClick(adapter,view,position)
        }
    }

    /**
     * 重复点击同一个时的事件
     */
    open fun repeatClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int){}


}